package org.csu.backofficemanagersys.controller;

import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import org.csu.backofficemanagersys.Response.ResponseMsg;
import org.csu.backofficemanagersys.domain.Admin;
import org.csu.backofficemanagersys.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/mypetstore/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/index")
    public ResponseMsg<List<Admin>> adminForm(){
        List<Admin> admins = adminService.selectAll();
        return ResponseMsg.createForSuccess(admins);
    }
    @GetMapping("/table")
    public ResponseMsg<JSONObject> find(int currentPage,int size,String adminname,String phone){
        List<Admin> admins;
        if(adminname !=null && phone != null){
            admins = adminService.findByNameAndPhone(adminname,phone);
        } else if (adminname == null && phone == null) {
            return SelectOneByName(currentPage,size);
        } else if (adminname == null) {
            admins = adminService.findByPhone(phone);
        } else {
            admins = adminService.findByName(adminname);
        }
        List<Admin> page ;
        int total = admins.size();
        if(total == 0){
            return ResponseMsg.createForSuccess("表格为空",null);
        }
        try {
            if( total<=size*currentPage-1 && total>=(currentPage-1)*size ){
                page = admins.subList(size * (currentPage - 1), total);
            }else {
                page = admins.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total",total);
            jsonObject.put("list",page);
            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }

    }
    //查找admin信息


    //添加admin信息
    @PostMapping("/table")
    public ResponseMsg<Objects> AddAdmin(Admin admin){
        if(adminService.addAdmin(admin)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    //根据id删除admin信息
    @DeleteMapping("/table/{id}")
    public ResponseMsg<Objects> deleteAdminById(@PathVariable int id){
        if(adminService.deleteAdminById(id)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    //更新admin信息
    @PutMapping ("/table")
    public ResponseMsg<Objects> updateAdmin(Admin admin){
        if(adminService.updateAdmin(admin.getAdminname(),admin.getPassword(),admin.getPhone())){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }

    @GetMapping("/info")
    public ResponseMsg<JSONObject> getInfo(HttpServletRequest request){
        Admin admin = (Admin) request.getSession().getAttribute("loginAdmin");
        JSONObject jsonObject = new JSONObject();
        int[] a= new int[1];
        a[0] =admin.getAuthority();
        jsonObject.put("adminname",admin.getAdminname());
        jsonObject.put("authority",a);
        return ResponseMsg.createForSuccess(jsonObject);
    }

    public ResponseMsg<JSONObject> SelectOneByName(int currentPage,int size){
        try {
            List<Admin> adminList = adminService.selectAll();
            JSONObject jsonObject = new JSONObject();
            List<Admin> admins;
            if(adminList.size()<size*currentPage && adminList.size()>(currentPage-1)*size){
                admins = adminList.subList(size * (currentPage - 1), adminList.size());
            }else {
                admins = adminList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",adminService.selectAll().size());
            jsonObject.put("list",admins);

            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }

    }
}
